package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.GameCarbon;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;
//副本类
public class UIGameCarbonWindow extends UIAbstractWindow
{

	public UIGameCarbonWindow(int id) {
		super(id);
		m_x = UITools.X_BG;
		m_y = UITools.Y_BG;
		m_w = UITools.W_BG;
		m_h = UITools.H_BG; 
	}
	
	

	protected void registerTouch() {
		registerNormalSoft();
		int size = AMOUNT_GAME_CARBON + 2;
		
		int[][] rect = new int[size][];
		for(int i=0;i<size;i++) {
			if (i == 0) {
				rect[i++] = new int[]{X_GAME_CARBON + W_GAME_CARBON, Y_GAME_CARBON,24,50};
				rect[i] = new int[]{X_GAME_CARBON + W_GAME_CARBON, Y_GAME_CARBON+H_GAME_CARBON*AMOUNT_GAME_CARBON-50,24,50};
			} else {
				rect[i] = new int[]{X_GAME_CARBON, Y_GAME_CARBON+(i-2)*H_GAME_CARBON,W_GAME_CARBON,H_GAME_CARBON};
			}
		}
		initTouchRect(rect);
	}



	protected void onTouchRect(int[][] rect, int touchid) {
		switch (touchid) {
		case 0:
			GameTouchManager.keyPressedDir(1);
			break;
		case 1:
			GameTouchManager.keyPressedDir(3);
			break;
		default:
			int select = touchid -2;
			if (m_game_carbon_index == select) {
				GameTouchManager.keyPressedFire();
//			} else if (m_game_carbon_index > touchid) {//点击上面的框。up
//				int offset = m_game_carbon_index - touchid;
			} else {
				//offset大于0，点击上面的框。up,减
				int offset = m_game_carbon_index - select;
				boolean add = offset < 0;
				ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
				if (gcl != null) {
					int len = Math.abs(offset);
					for (int i=0;i<len;i++) {
						byte[] result = UITools.keyPressedIndexPage(m_game_carbon_index, m_game_carbon_page, gcl.size(), AMOUNT_GAME_CARBON, !add);
						m_game_carbon_index = result[UITools.KPIP_INDEX];
						m_game_carbon_page = result[UITools.KPIP_PAGE];
					}
				}
			}
			break;
		}
	}
//	ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
//	if (gcl != null) {
//		byte[] result = UITools.keyPressedIndexPage(m_game_carbon_index, m_game_carbon_page, gcl.size(), AMOUNT_GAME_CARBON, UITools.OPERATION_MINUS);
//		m_game_carbon_index = result[UITools.KPIP_INDEX];
//		m_game_carbon_page = result[UITools.KPIP_PAGE];
//	}


	protected void paintContent(Graphics g) {
		switch (m_state) {
			case GC_GAME_CARBON:
				drawGameCarbon(g);
				break;
			case GC_TEAM:
				drawGCTeam(g);
				break;
		}
	}
	/**
	 * 绘制副本组队界面
	 * @param g
	 */
	private void drawGCTeam(Graphics g) {
		
	}

	/**
	 * 绘制副本界面
	 * @param g
	 */
	private void drawGameCarbon(Graphics g) {
		UITools.drawWindowBackgroud(g);
		drawGameCarbonContent(g);
		UITools.drawTitle(g, "副本");
		UITools.drawWindowSoftkey(g, UITools.BUTTON_INDEX_QUEDING, UITools.BUTTON_INDEX_FANHUI);
	}

	private void drawGameCarbonContent(Graphics g) {
		ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
		if (gcl != null) {
			if (gcl.size() > 0) {
				GameCarbon gc = null;
				for (int i = 0; i < AMOUNT_GAME_CARBON; i++) {
					if (i + m_game_carbon_page < gcl.size()) {
						gc = (GameCarbon)gcl.elementAt(i + m_game_carbon_page);
						if (gc != null) {
							gc.drawCarbon(g, X_GAME_CARBON, Y_GAME_CARBON + i * H_GAME_CARBON, W_GAME_CARBON, H_GAME_CARBON);
							gc.gun(i, m_game_carbon_index, W_GAME_CARBON_INFO);
						}
					}
				}
//				UITools.drawSelectBox(g, X_GAME_CARBON + 1, Y_GAME_CARBON + m_game_carbon_index * H_GAME_CARBON, W_GAME_CARBON - 1, H_GAME_CARBON - 1);
				paintSelBox(g, X_GAME_CARBON + 1, Y_GAME_CARBON + m_game_carbon_index * H_GAME_CARBON, W_GAME_CARBON - 1, H_GAME_CARBON - 1);
				g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
				UITools.drawColorString(g, GC_HINT_1, X_GC_HINT, Y_GC_HINT, Tools.WHITE, Tools.HCENTER_TOP);
				UITools.drawColorString(g, GC_HINT_2, X_GC_HINT, Y_GC_HINT + UITools.H_SINGLE_BAR,
						Tools.WHITE, Tools.HCENTER_TOP);
			} else {
				UITools.drawColorString(g, "无副本", UITools.X_BG + (UITools.W_BG >> 1), UITools.Y_BG + (UITools.H_BG >> 1), Tools.WHITE, Tools.HCENTER_TOP);
			}
		}
		//绘制滑动滚轴
		int tempX = X_GAME_CARBON + W_GAME_CARBON + 5;
		int tempY = Y_GAME_CARBON;
		UITools.paintScrollBar(g, tempX, tempY, AMOUNT_GAME_CARBON*H_GAME_CARBON, checkDragPlan());
		
//		Tools.debugPrintln("INFO_ALLPAGE="+INFO_ALLPAGE);
		// 绘制翻页
		if( INFO_ALLPAGE > 0 ){
			UITools.paintPage(g,UITools.X_BG+(UITools.W_BG>>1), UITools.Y_BG + UITools.H_BG - UITools.IW_WIN, checkIndexPage()-1, INFO_ALLPAGE,false,false);
		}	
	}
	private void paintSelBox(Graphics g, int x, int y, int w, int h) {
		paintSelBlur(g, x, y, w, h);
		int iw = 2;
		paintLanjiao(g, x - iw, y, w + (iw << 1), h);
	}

	public static void paintSelBlur(Graphics g, int x, int y, int w, int h) {
		int cx = g.getClipX();
		int cy = g.getClipY();
		int cw = g.getClipWidth();
		int ch = g.getClipHeight();
		int color = g.getColor();
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
		g.setColor(0x4f0000);
		g.drawRect(x - 4, y - 2, w + 7, h + 4);
		g.setColor(0x00ffea);
		g.drawRect(x - 3, y - 1, w + 5, h + 2);
		g.drawRect(x - 2, y, w + 3, h);
		g.drawImage(GameManager.getInstance().img_select, x, y + (h >> 1), Tools.RIGHT_VCENTER);
		Tools.drawRegion(g, GameManager.getInstance().img_select, x + w, y + (h >> 1), Tools.TRANS_H, Tools.LEFT_VCENTER);
		g.setClip(cx, cy, cw, ch);
		g.setColor(color);
	}

	public static void paintLanjiao(Graphics g, int x, int y, int w, int h) {
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x, y, Tools.TRANS_H90, Tools.LEFT_TOP);
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x + w, y, Tools.TRANS_H, Tools.RIGHT_TOP);
		int ih = 1;
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x, y + h + ih, Tools.TRANS_90, Tools.LEFT_BOTTOM);
		Tools.drawRegion(g, UIManager.IMAGE_LANJIAO, x + w, y + h + ih, Tools.TRANS_V90, Tools.RIGHT_BOTTOM);
	}
	protected void keyLogic() {
		switch (m_state) {
			case GC_GAME_CARBON:
				logicGameCarbon();
				break;
			case GC_TEAM:
				break;
		}
	}
	private void gameCarbonJoin(final String key) {
		final UIPopmenu pmenu = (UIPopmenu)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		pmenu.setPopmenus(new String[] {"1.自动组队", "2.进入副本"});
		pmenu.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
					int index = pmenu.getCurrentIndex();
					if (index == 0) {
						GameManager.getInstance().m_game_message.sendGameCarbonAuto(key);
					} else if (index == 1) {
						GameManager.getInstance().m_game_message.sendGameCarbonCheck(key);
					}
					UIManager.getInstance().removeWindow(pmenu);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(pmenu);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY1)) {
					GameManager.getInstance().m_game_message.sendGameCarbonAuto(key);
					UIManager.getInstance().removeWindow(pmenu);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY2)) {
					GameManager.getInstance().m_game_message.sendGameCarbonCheck(key);
					UIManager.getInstance().removeWindow(pmenu);
				}
			}
		});
	}
	private void gameCarbonAdd(final String key) {
		final UIMessageBox mb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		mb.setMessage("额外进入副本需要消耗一张[秘境地图],是否增次?");
		mb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
					GameManager.getInstance().m_game_message.sendGameCarbonAdd(key);
					UIManager.getInstance().removeWindow(mb);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(mb);
				}
			}
		});
	}
	private void gameCarbonLineUp(final String key) {
		final UIMessageBox mb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		mb.setMessage("是否确定取消排队?");
		mb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
					GameManager.getInstance().m_game_message.sendGameCarbonExit(key);
					UIManager.getInstance().removeWindow(mb);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(mb);
				}
			}
		});
	}
	private void logicGameCarbon() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
			if (gcl != null && m_game_carbon_page + m_game_carbon_index < gcl.size()) {
				GameCarbon gc = (GameCarbon)gcl.elementAt(m_game_carbon_page + m_game_carbon_index);
				if (gc.getStatus() == gc.STATUS_JOIN) {
					if (gc.getLv() > GameManager.getInstance().m_role.m_lv) {
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "等级不够,进入此副本需要" + gc.getLv() + "级!");
					} else {
						gameCarbonJoin(gc.getKey());
					}
				} else if (gc.getStatus() == gc.STATUS_ADD) {
					gameCarbonAdd(gc.getKey());
				} else if (gc.getStatus() == gc.STATUS_LINE_UP) {
					gameCarbonLineUp(gc.getKey());
				}
				
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			UIManager.getInstance().removeWindow(this);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
			if (gcl != null) {
				byte[] result = UITools.keyPressedIndexPage(m_game_carbon_index, m_game_carbon_page, gcl.size(), AMOUNT_GAME_CARBON, UITools.OPERATION_MINUS);
				m_game_carbon_index = result[UITools.KPIP_INDEX];
				m_game_carbon_page = result[UITools.KPIP_PAGE];
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
			if (gcl != null) {
				byte[] result = UITools.keyPressedIndexPage(m_game_carbon_index, m_game_carbon_page, gcl.size(), AMOUNT_GAME_CARBON, UITools.OPERATION_ADD);
				m_game_carbon_index = result[UITools.KPIP_INDEX];
				m_game_carbon_page = result[UITools.KPIP_PAGE];
			}
		}
	}
	/**
	 * 打开副本界面
	 */
	private static void gameCarbonOpen() {
		UIGameCarbonWindow gcw = (UIGameCarbonWindow)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_GAMECARBON);
		if (gcw == null) {
			gcw = (UIGameCarbonWindow)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_ID_GAMECARBON);
		}
		ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
		int num = -1;
		if( gcl != null && gcl.size() > 0){
			num = gcl.size();
		}
		gcw.INFO_ALLNUM = num;
	    int n = num%gcw.AMOUNT_GAME_CARBON;
	    int m = num/gcw.AMOUNT_GAME_CARBON;
	    if( m < 1){
	    	m = 1;
	    }
	    if( n > 0 ){
	    	m +=1;
	    }
		gcw.INFO_ALLPAGE = m;
		gcw.setWindowStatus(gcw.GC_GAME_CARBON);
	}
	/**
	 * 接收副本界面开启
	 * @param dis
	 * @throws IOException
	 */
	public static final void recvGameCarbonOpen(DataInputStream dis) throws IOException {
		byte size = dis.readByte();//副本数量
		if (size > 0) {
			ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
			gcl.removeAllElements();
			for (byte i = 0; i < size; i++) {
				GameCarbon gc = new GameCarbon();
				gc.read(dis);
				gcl.addElement(gc);
			}
			gameCarbonOpen();
		}
	}
	/**
	 * 副本通关提示
	 * @param dis
	 * @throws IOException
	 */
	public static void recvGameCarbonFinish(DataInputStream dis) throws IOException {
		String gcName = dis.readUTF();
		String gcReward = dis.readUTF();
		Tools.debugPrintln("副本 : " + gcName + ", 奖励 : " + gcReward);
		final UIMessageBox umb = (UIMessageBox) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		umb.setMessage("胜利", "", null, "恭喜您完成" + gcName + "!" + Tools.CHANGE_ROW + "获得" + gcReward + "!");
		umb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					UIManager.getInstance().removeWindow(umb);
				}
			}
		});
	}
	/**
	 * 接收副本检测
	 * @param dis
	 */
	public static void recvGameCarbonCheck(DataInputStream dis) throws IOException {
		byte status = dis.readByte();
		Tools.debugPrintln("副本进入检测 : " + status);
	}
	/**
	 * 接收自动组队
	 * @param dis
	 * @throws IOException
	 */
	public static void recvGameCarbonAuto(DataInputStream dis) throws IOException {
		final byte status = dis.readByte();
		String info = dis.readUTF();
		final UIMessageBox mb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		mb.setMessage("提示", status == 0 ? null : "确定", status == 0 ? "返回" : null, info);
		mb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
					if (status == 1) {
						UIManager.getInstance().removeWindow(mb);
					}
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					if (status == 0) {
						UIManager.getInstance().removeWindow(mb);
					}
				}
			}
		});
	}
	/**
	 * 更新副本的状态
	 * @param dis
	 * @throws IOException
	 */
	public static void recvGameCarbonUpdate(DataInputStream dis) throws IOException {
		byte size = dis.readByte();
		if (size > 0) {
			ArrayList gcl = GameManager.getInstance().m_role.m_game_carbon;
			GameCarbon gc = null;
			for (int i = 0, j = 0; i < size; i++) {
				String key = dis.readUTF();
				byte status = dis.readByte();
				for (j = 0; j < gcl.size(); j++) {
					gc = (GameCarbon)gcl.elementAt(j);
					if (gc.getKey().equals(key)) {
						gc.updateStatus(status);
						continue;
					}
				}
			}
		}
	}
	/**
	 * 副本增次
	 * @param dis
	 * @throws IOException
	 */
	public static void recvGameCarbonAdd(DataInputStream dis) throws IOException {
		dis.readByte();
	}
	
	private byte m_game_carbon_index = 0;
	private byte m_game_carbon_page = 0;
	//副本界面单个副本
	private final short X_GAME_CARBON = UITools.X_BG + UITools.GAP_WINDOW_SUBCOAT_LEFT + 5;
	private final short Y_GAME_CARBON = UITools.Y_BG + UITools.H_TITLE + UITools.GAP_WINDOW_SUBCOAT_UP;
	private final short W_GAME_CARBON = UITools.W_BG - UITools.GAP_WINDOW_SUBCOAT_LEFT - UITools.GAP_WINDOW_SUBCOAT_RIGHT - 15;
	private final short H_GAME_CARBON = (short)((UITools.H_SINGLE_BAR + UITools.GAP_EACH_LINE) * 3);
	private final int W_GAME_CARBON_INFO = (W_GAME_CARBON - AbstractGameScreen.s_font.stringWidth("介绍 : ") - 4);
	//副本界面下方提示 - 中上绘制
	private final short X_GC_HINT = UITools.X_BG + (UITools.W_BG >> 1);
	private final short Y_GC_HINT = (short)(UITools.Y_BG + UITools.H_BG - UITools.H_BUTTON - (UITools.H_SINGLE_BAR << 1));
	private final String GC_HINT_1 = "每天可使用门票";
	private final String GC_HINT_2 = "增加1次进入机会";
	//副本界面绘制数量
	private final short H_GC_TRUE = (short)(Y_GC_HINT - Y_GAME_CARBON);
	public final byte AMOUNT_GAME_CARBON = (byte)(H_GC_TRUE / H_GAME_CARBON);
	
	
	public final byte GC_GAME_CARBON = 0;//副本界面
	public final byte GC_TEAM = 1;//副本组队界面
	
	//内容总条数
	public int INFO_ALLNUM = 0;//内容总条数
	public int INFO_ALLPAGE = 1;//总页数
	
	
	protected int checkIndexPage(){
		int n = m_game_carbon_index + m_game_carbon_page+1;
		int num = n/AMOUNT_GAME_CARBON;
		int m = n%AMOUNT_GAME_CARBON;
		if( num < 1){
			return 1;
		}
		if( m > 0 ){
	        num += 1;
		}
		return num;
	}
	protected int checkDragPlan(){
		if( INFO_ALLNUM == -1){
			return INFO_ALLNUM;
		}
		int max = INFO_ALLNUM <= 1?2:INFO_ALLNUM;
	    return (m_game_carbon_index+m_game_carbon_page)*100/(max-1);
	}
}

